<template>
  <div>
    <el-card class="cardBox">
      <el-breadcrumb separator="/">
          <el-breadcrumb-item>首页</el-breadcrumb-item>
          <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="operarea">
        <div class="btnList">
          <el-button size="mini" @click="append('0', '')">{{
            $t("add")
          }}</el-button>
        </div>
      </div>
      <div class="block">
        <el-table style="width: 100%" row-key="id" :data="menuTab" :tree-props="{children: 'children', hasChildren: '!hasChildren'}">
          <el-table-column prop="text" :label="$t('menuname')">
          </el-table-column>
          <el-table-column :label="$t('operation')" width="250">
              <template slot-scope="scope">
                  <el-button v-if="scope.row.type === '0'" type="primary" size="mini" @click="append(scope.row.id, scope.row.parentId)">{{$t('add')}}</el-button>
                  <el-button type="primary" size="mini" @click="edit(scope.row.id)">{{$t('modify')}}</el-button>
                  <el-button type="primary" size="mini" @click="remove(scope.row)">{{$t('delete')}}</el-button>
              </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <!-- 添加 -->
    <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addMenuForm)">
      <el-form :model="addMenuForm" :rules="addRules" :ref="addMenuForm" class="motuoForm">
        <el-form-item :label="$t('menuname')" label-width="70px" prop="menuName">
          <el-input v-model="addMenuForm.menuName" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item>
        <el-form-item label="url" label-width="70px" prop="url">
          <el-input v-model="addMenuForm.url" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item>
        <!-- <el-form-item :label="$t('icon')" label-width="70px" prop="icon">
          <el-input v-model="addMenuForm.icon" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">{{$t("cancel")}}</el-button>
        <el-button type="primary" @click="addData(addMenuForm)">{{$t("confirm")}}</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyMenuForm)">
      <el-form :model="modifyMenuForm" :rules="modifyRules" :ref="modifyMenuForm" class="motuoForm">
        <el-form-item :label="$t('menuname')" label-width="70px" prop="menuName">
          <el-input v-model="modifyMenuForm.menuName" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item>
        <el-form-item label="url" label-width="70px" prop="url">
          <el-input v-model="modifyMenuForm.url" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item>
        <!-- <el-form-item :label="$t('icon')" label-width="70px" prop="icon">
          <el-input v-model="modifyMenuForm.icon" clearable :placeholder="$t('plsent')"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyDialogVisible = false">{{$t("cancel")}}</el-button>
        <el-button type="primary" @click="modifyData(modifyMenuForm)">{{$t("confirm")}}</el-button>
      </span>
    </el-dialog>
    <!-- 删除 -->
    <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
        <span>{{$t('confirmdel')}}</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
            <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMenuTree,
  addMenu,
  getMenuDetails,
  removeMenu
} from '@/libs/reqlist.js';
export default {
  data() {
    return {
      selMenuForm: {
        menuName: "",
      },
      menuTab: [],
      addMenuForm: {
        pid: "",
        type: 0,
        menuName: "",
        url: "",
        icon: "",
        perms: "sys:menu"
      },
      addRules: {
        menuName: [
          { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        ],
        url: [
          { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        ],
        // icon: [
        //   { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        // ]
      },
      addDialogVisible: false,
      modifyDialogVisible: false,
      modifyMenuForm: {
        id: "",
        pid: "",
        type: 0,
        menuName: "",
        url: "",
        icon: "",
        perms: "sys:menu"
      },
      modifyRules: {
        menuName: [
          { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        ],
        url: [
          { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        ],
        // icon: [
        //   { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
        // ]
      },
      delDialogVisible: false,
      removeId: {}
    };
  },
  methods: {
    seachData() {
      getMenuTree().then(res => {
        if (res.data.code === 0) this.menuTab = res.data.data
      })
    },
    append(fid, btn) {
      switch (fid) {
        case "0":
          delete this.addMenuForm.pid
          break;
        default:
          this.addMenuForm.pid = fid;
      }
      switch (btn) {
        case "0":
          this.addMenuForm.type = 0;
          break;
        case "":
          this.addMenuForm.type = 0;
          break;
        default:
          this.addMenuForm.type = 1;
          break;
      }
      this.addDialogVisible = true;
    },
    remove (city) {
      this.removeId = city
      this.delDialogVisible = true
    },
    addData(form) {
      this.$refs[form].validate((valid) => {
        if (!valid) return;
        addMenu(JSON.stringify(this.addMenuForm)).then(res => {
          if (res.data.code === 0) {
            this.addDialogVisible = false;
            this.$message({
              type: "success",
              message: this.$t("sucadd"),
            });
            this.seachData()
          }
        })
      });
    },
    addDlogClose(form) {
      this.$refs[form].resetFields();
    },
    edit(itemId) {
      getMenuDetails(itemId).then(res => {
        if (res.data.code === 0) {
          this.modifyMenuForm.id = res.data.data.id
          this.modifyMenuForm.pid = res.data.data.pid
          this.modifyMenuForm.type = res.data.data.type
          this.modifyMenuForm.menuName = res.data.data.menuName
          this.modifyMenuForm.url = res.data.data.url
          this.modifyMenuForm.icon = res.data.data.icon
        }
      })
      this.modifyDialogVisible = true
    },
    modifyDlogClose (form) {
      this.$refs[form].resetFields()
    },
    modifyData (form) {
      this.$refs[form].validate((valid) => {
        if (!valid) return
        addMenu(JSON.stringify(this.modifyMenuForm)).then(res => {
          if (res.data.code === 0) {
            this.modifyDialogVisible = false
            this.$message({
              type: 'success',
              message: this.$t('modifysuc')
            })
            this.seachData()
          }
        })
      })
    },
    delData () {
      removeMenu(JSON.stringify(this.removeId)).then(res => {
        if (res.data.code === 0) {
          this.$message({
            type: 'success',
            message: this.$t('delsuc')
          })
          this.delDialogVisible = false
          this.seachData()
        }
      })
    }
  },
  mounted () {
    this.seachData()
  }
};
</script>

<style lang="less" scoped>
@import "./menu.less";
</style>
